import Request from '@/uni_modules/luch-request/luch-request/index.js'
import { BASE_API } from '@/common/http/url.js'

const http = new Request({
  baseURL: BASE_API,
  timeout: 300000,
  header: {
    'Content-Type': 'application/json;charset=UTF-8;',
  }
})

http.setConfig((config) => {
	config.baseURL = '/pc' 
	config.header = {
		'Content-Type': 'application/json;charset=UTF-8'
	}
	return config
})


http.interceptors.request.use((config) => { 
	let token = uni.getStorageSync('user');
	if (token) {
		config.header = {
			"Authorization": token.token
		}
	}

	if (config.method === 'POST') {
		config.data = JSON.stringify(config.data);
	}
	return config
}, error => {
	return Promise.resolve(error)
})


http.interceptors.response.use((response) => {
	console.log(response)
	return response
}, (error) => {
	
	if (error.statusCode == 401) {
		uni.clearStorageSync();
		uni.switchTab({
			url: "/pages/index/index.vue"
		})
	}
	return Promise.resolve(error)
})

export default http;